//
//  VoiceFollowMaskView.m
//  for_test
//
//  Created by Carlson Lee on 2017/4/5.
//  Copyright © 2017年 Carlson Lee. All rights reserved.
//

#import "VoiceFollowMaskView.h"

@interface VoiceFollowMaskView ()

@property(nonatomic, strong)CAShapeLayer* shapeFace;
@property(nonatomic, strong)CAShapeLayer* shapeLine;

@end

@implementation VoiceFollowMaskView

- (CAShapeLayer *)shapeFace
{
    if(!_shapeFace){
        _shapeFace  =[CAShapeLayer layer];
        [self.layer addSublayer:_shapeFace];
        [_shapeFace setLineWidth:2.0];
        [_shapeFace setStrokeColor:[UIColor clearColor].CGColor];
        [_shapeFace setFillColor:AlphaColorWithRGBA(0x000000, .5).CGColor];
        _shapeFace.contentsScale = [UIScreen mainScreen].scale;
        _shapeFace.fillRule = kCAFillRuleEvenOdd;
    }
    return _shapeFace;
}

- (CAShapeLayer *)shapeLine
{
    if(!_shapeLine){
        _shapeLine  =[CAShapeLayer layer];
        [self.layer addSublayer:_shapeLine];
        [_shapeLine setLineWidth:2.0];
        [_shapeLine setStrokeColor:UIColorFromRGB(0xffa418).CGColor];
        [_shapeLine setFillColor:[UIColor clearColor].CGColor];
        _shapeLine.contentsScale = [UIScreen mainScreen].scale;
        [_shapeLine setLineDashPattern:@[@(8), @(5)]];
    }
    return _shapeLine;
}

- (UIBezierPath* )facePathWithRect:(CGRect )rt skew:(CGPoint )pt
{
    UIBezierPath* bezier = [UIBezierPath bezierPath];
    [bezier moveToPoint:CGPointMake(10+pt.x, CGRectGetHeight(rt)/3+10+pt.y)];
    [bezier addQuadCurveToPoint:CGPointMake(CGRectGetWidth(rt)/2+pt.x, 0+pt.y) controlPoint:CGPointMake(20+pt.x, 5+pt.y)];
    [bezier addQuadCurveToPoint:CGPointMake(CGRectGetWidth(rt)-10+pt.x, CGRectGetHeight(rt)/3+10+pt.y) controlPoint:CGPointMake(CGRectGetWidth(rt)-20+pt.x, 5+pt.y)];
    [bezier addQuadCurveToPoint:CGPointMake(CGRectGetWidth(rt)/2+pt.x, CGRectGetHeight(rt)+pt.y) controlPoint:CGPointMake(CGRectGetWidth(rt)-20+pt.x, CGRectGetHeight(rt)-20+pt.y)];
    [bezier addQuadCurveToPoint:CGPointMake(10+pt.x, CGRectGetHeight(rt)/3+10+pt.y) controlPoint:CGPointMake(20+pt.x, CGRectGetHeight(rt)-20+pt.y)];
    return bezier;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.shapeFace.frame = self.bounds;
    
    
    CGRect rt = CGRectMake((UIFrameW(self)-280*ScaleX)/2, 50*ScaleX, 280*ScaleX, 350*ScaleX);
    UIBezierPath* bezier = [UIBezierPath bezierPathWithRect:self.bounds];
    [bezier appendPath:[self facePathWithRect:rt skew:CGPointMake((UIFrameW(self)-280*ScaleX)/2, 50*ScaleX)]];
    self.shapeFace.path = bezier.CGPath;
    self.shapeLine.frame = rt;
    self.shapeLine.path = [self facePathWithRect:rt skew:CGPointZero].CGPath;
    
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.


@end
